package com.lanou.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.lanou.pojo.*;
import com.lanou.service.prePaymentService;
import com.lanou.utils.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@Api(value = "预存款管理", tags = "收银")
public class PrePaymentController {
    @Resource
    private prePaymentService PaymentService;

    @ApiOperation(value = "预存款获取数据列表", httpMethod = "GET")
    @RequestMapping(value = "/selectPaymentPreList", produces = "text/html;charset=utf-8")
    public String selectPaymentPre(String pageNum, String pageSize, String resourceName, String feeUser, String type, String feeItemName, String payType) {
        List<PaymentPreEntity> paymentPreEntities = PaymentService.selectPaymentPreList(pageNum, pageSize, resourceName, feeUser, type, feeItemName, payType);

        PageInfo info = new PageInfo<>(paymentPreEntities);

        JsonResult jsonResult = new JsonResult<>();
        jsonResult.setStatus(200);
        jsonResult.setData(info);
        return JSON.toJSONString(jsonResult);
    }

    @ApiOperation(value = "预存款添加数据", httpMethod = "POST")
    @RequestMapping("/addPrePayment")
    public void addPrePayment(@RequestBody Map<String, String> requestbody) throws ParseException {
        String id = requestbody.get("id");
        String name = requestbody.get("name");
        String chargeitem = requestbody.get("chargeitem");
        String paymethod = requestbody.get("paymethod");
        String money = requestbody.get("money");
        String date1 = requestbody.get("date1");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(date1);
        System.out.println(requestbody);
        PaymentService.addPrePayment(id, name, chargeitem, paymethod, money, date);
    }

    @ApiOperation(value = "预存款查询账号信息", httpMethod = "GET")
    @RequestMapping(value = "/selectPayPreAccountMessage", produces = "text/html;charset=utf-8")
    public String selectPaymentPreAccountMessage(String resourceName) {
        List<PaymentPreAccountEntity> paymentPreAccountEntities = PaymentService.selectPaymentPreAccountMessage(resourceName);
        PageInfo info = new PageInfo<>(paymentPreAccountEntities);
        JsonResult jsonResult = new JsonResult<>();
        jsonResult.setStatus(200);
        jsonResult.setData(info);

        return JSON.toJSONString(jsonResult);

    }

    @ApiOperation(value = "预存款更新账号信息", httpMethod = "POST")
    @RequestMapping("/updatePrePaymentAccount")
    public void updatePrePayment(@RequestBody Map<String, String> requestbody) throws ParseException {
        String resourceName = requestbody.get("resourceName");
        String payMethod = requestbody.get("payMethod");
        String time = requestbody.get("backTime");
        String backmoney = requestbody.get("backmoney");
        String itemId = requestbody.get("ItemId");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(time);
        System.out.println(requestbody);
        PaymentService.updatePayMessage(resourceName, payMethod, date, backmoney, itemId);

    }

    @RequestMapping(value = "/selectShopMessageList", produces = "text/html;charset=utf-8")
    public String selectShopMessageList(String pageNum, String pageSize) {
        List<ConfigHouseEntity> configHouseEntities = PaymentService.selectShopMessageList(pageNum, pageSize);
        PageInfo info = new PageInfo<>(configHouseEntities);
        JsonResult jsonResult = new JsonResult<>();
        jsonResult.setStatus(200);
        jsonResult.setData(info);
        return JSON.toJSONString(jsonResult);
    }

//    @ApiOperation(value = "商铺管理文件上传", httpMethod = "POST")
//    @RequestMapping(value = "/upload", produces = "text/html;charset=utf-8")
//    public String getUploadFile(@RequestParam("file") MultipartFile multipartFile) {
//        try {
//            String uploadPath = "E:\\ZHWY\\zhwy\\AfterEnd\\ZHWY-parent\\ZHWY-controller\\src\\main\\resources\\tempFile";
//            String fileName = multipartFile.getOriginalFilename();
//            File file = new File(uploadPath + File.separator + fileName);
//            multipartFile.transferTo(file);
//            return PaymentService.uploadFile(multipartFile);
//        } catch (Exception e) {
//            e.printStackTrace();
//            return "文件上传失败";
//        }
//    }

    @ApiOperation(value = "商铺管理商铺导入模板下载", httpMethod = "POST")
    @RequestMapping("/download")
    public ResponseEntity<FileSystemResource> downloadTemplate() {
        try {
            FileSystemResource classPathResource = new FileSystemResource( "/opt/tomcat/apache-tomcat-9.0.29/webapps/ROOT/WEB-INF/classes/商铺导入模板.xlsx");
            HttpHeaders httpHeaders = new HttpHeaders();
            httpHeaders.add(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=商铺导入模板.xlsx");

            return ResponseEntity.ok()
                    .headers(httpHeaders)
                    .body(classPathResource);
        } catch (Exception e) {
            System.err.println("无法读取模板文件: " + e.getMessage());
            return ResponseEntity.badRequest().build();
        }
    }
}
